package models

type Paginator struct {
	PageNo     int `json:"current_page"`//当前页
	PageSize   int `json:"-"`//每页多少数据
	TotalPage  int `json:"total"`//总共多生页
	TotalCount int `json:"count"`//总共多少数据
	FirstPage  int `json:"first_page"`//第一页
	LastPage   int `json:"last_page"`//最后一页
	Limit      int `json:"-"`//数据库limit
	Offset     int `json:"-"`//数据库offset
}

func NewPage(pageNo int, pageSize int, totalCount int) Paginator {
	p := Paginator{PageNo: pageNo, PageSize: pageSize, TotalCount: totalCount}
	var tp = float32(p.TotalCount) / float32(p.PageSize)
	if tp < 1 {
		p.TotalPage = 1
	}
	var tpint = float32(int(tp))

	if tp > tpint {
		tpint += 1
	}

	p.TotalPage = int(tpint)
	p.LastPage = int(tpint)
	p.FirstPage = 1

	p.Limit = pageSize
	p.Offset = (pageNo - 1) * pageSize

	return p
}
